import {
  queryTeacherList,
  addTeacherList,
  updateTeacherList,
  removeTeacherList,
  queryTeacherDetail,
} from '@/services/api';

export default {
  namespace: 'teachers',

  state: {
    data: {
      // 教师列表
      list: [],
      pagination: {},
    },
    profile: {}, // 教师详情
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryTeacherList, payload);

      yield put({
        type: 'query',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addTeacherList, payload);

      yield put({
        type: 'query',
        payload: response,
      });
      if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeTeacherList, payload);
      yield put({
        type: 'query',
        payload: response,
      });
      if (callback) callback();
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateTeacherList, payload);

      yield put({
        type: 'query',
        payload: response,
      });
      if (callback) callback();
    },

    *fetchById({ payload, callback }, { call, put }) {
      const response = yield call(queryTeacherDetail, payload);

      yield put({
        type: 'queryOne',
        payload: response,
      });

      if (callback) callback();
    },
  },

  reducers: {
    query(state, { payload }) {
      return {
        ...state,
        data: payload,
      };
    },

    queryOne(state, action) {
      return {
        ...state,
        profile: action.payload,
      };
    },
  },
};
